package com.ruoyi.web.controller.cs;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BasicsController;
import com.ruoyi.common.core.domain.Response;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.ResponsePageInfo;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.cs.domain.TUserFeedback;
import com.ruoyi.cs.domain.vo.TUserSyVo;
import com.ruoyi.cs.service.ITUserService;
import com.ruoyi.cs.service.ITUserTgService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.mapper.SysConfigMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.cs.domain.TUserSy;
import com.ruoyi.cs.service.ITUserSyService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户推广收益记录Controller
 * 
 * @author ruoyi
 * @date 2021-03-25
 */
@Api(tags = "用户推广收益记录")
@RestController
@RequestMapping("/cs/usersy")
@SuppressWarnings("all")
public class TUserSyController extends BasicsController
{
    @Autowired
    private ITUserSyService tUserSyService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private SysConfigMapper sysConfigMapper;

    @Autowired
    private ITUserTgService userTgService;

    /**
     * 查询用户推广收益记录列表
     */
    @ApiOperation("查询用户推广收益记录列表")
    @PreAuthorize("@ss.hasPermi('cs:sy:listBack')")
    @RequestMapping(name="查询用户推广收益记录列表",value="listBack",method = RequestMethod.POST)
    public ResponsePageInfo<Map<String, Object>> listBack(@RequestBody TUserSyVo tUserSy)
    {
        startPage(tUserSy.getPageNum(),tUserSy.getPageSize());
        List<TUserSyVo> list = tUserSyService.userSyList(tUserSy);

        BigDecimal total = new BigDecimal(0);
        BigDecimal sjtTotal = new BigDecimal(0);
        if (list.size() > 0) {
            for (TUserSyVo userSyVo : list) {
                /*total = total.add(userSyVo.getActualRecharge());*/
                sjtTotal = sjtTotal.add(userSyVo.getCzMoney());
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("sjTotal",sjtTotal);
        return getDataTableObj(map,list);
    }


    /**----------------------------------------------前端---------------------------------------------*/
    /**
     * 个人用户推广收益记录列表
     */
    @ApiOperation("web端个人用户推广收益记录列表")
    @RequestMapping(name="个人用户推广收益记录列表",value="list",method = RequestMethod.POST)
//    public ResponsePageInfo<Map<String, Object>> list(@RequestBody TUserSyVo tUserSy)
    public ResponsePageInfo<Map<String, Object>> list(@RequestBody TUserSyVo tUserSy)
    {
        LoginUser loginUser = tokenService.getLoginUserWeb(ServletUtils.getRequest());
        SysUser sysUser = loginUser.getUser();
        String userId = sysUser.getLoginUserId();
        SysConfig yqzsblConfig = sysConfigMapper.checkConfigKeyUnique("invitation_to_recharge");
        String yqzsbl = yqzsblConfig.getConfigValue() == null ? "0.03" : yqzsblConfig.getConfigValue();
        startPage(tUserSy.getPageNum(), tUserSy.getPageSize());
        List<TUserSyVo> list = tUserSyService.userSyListWeb(userId, null);
        Map tgTotal = tUserSyService.tgSyTotal(userId);
        /**获取总邀请人数*/
        Long tgtotal = userTgService.tgTotal(userId);
        BigDecimal total = new BigDecimal(0);
        if (list.size() > 0) {
            for (TUserSyVo bean : list) {
                total = total.add(BigDecimal.valueOf(Double.parseDouble(bean.getSyMoney().toString())));
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("total",tgTotal.get("tgSyTotal"));
        map.put("tgZczTotal",tgTotal.get("tgZczTotal"));
        map.put("tgPeopleNum", tgtotal);
        map.put("rewardProportion", new BigDecimal(yqzsbl).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP));
        return getDataTableObj(map, list);
    }
}